<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">用户管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>用户管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option value="">-请选择-</option>
                <option value="user_id">ID</option>
                <option value="username">账号</option>
                <option value="nick_name">用户名</option>
                <option value="phone">手机号</option>
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="user-btn-add" class="layui-btn icon-btn" lay-tips="新用户密码为123456"><i
                    class="layui-icon">&#xe654;</i>添加
            </button>
        </div>

        <table class="layui-table" id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="user-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.userId}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.state==0?'checked':''}}/>
</script>
<script>
    layui.use(['form', 'table','layer', 'util', 'admin', 'element'], function () {
            var form = layui.form;
            var table = layui.table;
            var layer = layui.layer;
            var util = layui.util;
            var admin = layui.admin;
            var element = layui.element;
            
            form.render('select');

         // 渲染表格
            table.render({
                elem: '#user-table',
                url: '/system/user/list',
                page: true,
                cols: [
                	[
                    {type: 'numbers'},
                    {field: 'username', sort: true, title: '账号'},
                    {field: 'nickName', sort: true, title: '用户名'},
                    {field: 'phone', sort: true, title: '手机号'},
                    {field: 'sex', sort: true, title: '性别'},
                    {
                        sort: true, templet: function (d) {
                            return util.toDateString(d.createTime);
                        }, title: '创建时间'
                    },
                    {field: 'state', sort: true, templet: '#user-tpl-state', title: '状态'},
                    {align: 'center', toolbar: '#user-table-bar', title: '操作'}
                ]
                	]
            });
         
            // 添加按钮点击事件
            $('#user-btn-add').click(function () {
                showEditModel();
            });
            
            
            // 工具条点击事件
            table.on('tool(user-table)', function (obj) {
            	console.log(obj);
                var data = obj.data;
                var layEvent = obj.event;

                if (layEvent === 'edit') { // 修改
                    showEditModel(data);
                } else if (layEvent === 'reset') { // 重置密码
                    layer.confirm('确定重置此用户的密码吗？', function (i) {
                        layer.close(i);
                        layer.load(2);
                        admin.go('/system/user/restPsw', {
                            userId: obj.data.userId
                        }, function (data) {
                        	console.log(data.code);
                            layer.closeAll('loading');
                            if (data.code == 200) {
                                layer.msg(data.msg, {icon: 1});
                            }else if(data.code == 401){
                            	layer.msg(data.msg, {icon: 2, time: 1500}, function () {location.replace('/login'); }, 1000);
                            	 return;
                            }
                            else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        });
                    });
                }
            });
            
            // 显示表单弹窗
            var showEditModel = function (data) {
                var title = data ? '修改用户' : '添加用户';
                admin.putTempData('t_user', data);
                admin.popupCenter({
                    title: title,
                    path: '/system/user/editForm',
                    finish: function () {
                        table.reload('user-table', {});
                    }
                });
            };
            
            // 搜索按钮点击事件
            $('#user-btn-search').click(function () {
                var key = $('#user-search-key').val();
                var value = $('#user-search-value').val();
                table.reload('user-table', {where: {searchKey: key, searchValue: value}});
            });
            
            // 修改user状态
            form.on('switch(user-tpl-state)', function (obj) {
                layer.load(2);
                admin.go('/system/user/updateState', {
                    userId: obj.elem.value,
                    state: obj.elem.checked ? 0 : 1
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 1});
                        //table.reload('table-user', {});
                    }else if(data.code == 401){
                    	layer.msg(data.msg, {icon: 2, time: 1500}, function () {location.replace('/login'); }, 1000);
                   	 return;
                   } else {
                        layer.msg(data.msg, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                });
            });
         
            });
    </script> 
